namespace cnVery.Cms
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.WebControls;

    public class orders
    {
        // Methods
        public static int Add(order model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into orders(");
            builder.Append("username,ordernumber,name,address,zipcode,phone,email,payment,payed,price,remark,type,disposeremark,carryway,carryprice,userid,audit,send");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.username + "',");
            builder.Append("'" + model.ordernumber + "',");
            builder.Append("'" + model.name + "',");
            builder.Append("'" + model.address + "',");
            builder.Append("'" + model.zipcode + "',");
            builder.Append("'" + model.phone + "',");
            builder.Append("'" + model.email + "',");
            builder.Append("'" + model.payment + "',");
            builder.Append("" + (model.payed ? 1 : 0) + ",");
            builder.Append("" + model.price + ",");
            builder.Append("'" + model.remark + "',");
            builder.Append("" + model.type + ",");
            builder.Append("'" + model.disposeremark + "',");
            builder.Append("'" + model.carryway + "',");
            builder.Append("" + model.carryprice + ",");
            builder.Append("" + model.userid + ",");
            builder.Append("" + (model.audit ? 1 : 0) + ",");
            builder.Append("" + (model.send ? 1 : 0) + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            string sql = "SELECT @@IDENTITY AS Expr";
            object scalar = connection.GetScalar(sql);
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return int.Parse(scalar.ToString());
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete orders ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            string sql = "delete details where orderid=" + id.ToString();
            connection.Execute(sql);
            connection.Close();
        }

        public static void Delete(string ids)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete orders ");
            builder.Append(" where ordernumber in(" + ids + ")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            string sql = "delete details where ordernumber in(" + ids + ")";
            connection.Execute(sql);
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from orders where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int)scalar;
            return (num > 0);
        }

        public static int GetCount(string filter)
        {
            if (filter == "")
            {
                filter = "0=0";
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from orders where " + filter);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return (int)scalar;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from orders ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(int pageindex, int pagesize, string filter)
        {
            if (pageindex <= 0)
            {
                return null;
            }
            if (filter == "")
            {
                filter = "0=0";
            }
            string sql = string.Concat(new object[] { "select top ", pagesize, " * from orders where ", filter });
            if (pageindex > 1)
            {
                object obj2 = sql;
                sql = string.Concat(new object[] { obj2, " and id < (select min(id) from (select top ", pagesize * (pageindex - 1), " id from orders where ", filter, " order by id desc) as tb)" });
            }
            sql = sql + " order by id desc ";
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(sql);
            connection.Close();
            return dataTable;
        }

        public static order GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from orders ");
            builder.Append(" where id=" + id);
            order order = new order();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            order.id = id;
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            order.username = dataTable.Rows[0]["username"].ToString();
            order.ordernumber = dataTable.Rows[0]["ordernumber"].ToString();
            order.name = dataTable.Rows[0]["name"].ToString();
            order.address = dataTable.Rows[0]["address"].ToString();
            order.zipcode = dataTable.Rows[0]["zipcode"].ToString();
            order.phone = dataTable.Rows[0]["phone"].ToString();
            order.email = dataTable.Rows[0]["email"].ToString();
            order.payment = dataTable.Rows[0]["payment"].ToString();
            order.userid = int.Parse(dataTable.Rows[0]["userid"].ToString()); //1.1
            if (dataTable.Rows[0]["audit"].ToString() != "") //1.1
            {
                if ((dataTable.Rows[0]["audit"].ToString() == "1") || (dataTable.Rows[0]["audit"].ToString().ToLower() == "true"))
                {
                    order.audit = true;
                }
                else
                {
                    order.audit = false;
                }
            }
            if (dataTable.Rows[0]["send"].ToString() != "") //1.1
            {
                if ((dataTable.Rows[0]["send"].ToString() == "1") || (dataTable.Rows[0]["send"].ToString().ToLower() == "true"))
                {
                    order.audit = true;
                }
                else
                {
                    order.audit = false;
                }
            }
            if (dataTable.Rows[0]["payed"].ToString() != "")
            {
                if ((dataTable.Rows[0]["payed"].ToString() == "1") || (dataTable.Rows[0]["payed"].ToString().ToLower() == "true"))
                {
                    order.payed = true;
                }
                else
                {
                    order.payed = false;
                }
            }
            if (dataTable.Rows[0]["paytime"].ToString() != "")
            {
                order.paytime = DateTime.Parse(dataTable.Rows[0]["paytime"].ToString());
            }
            if (dataTable.Rows[0]["price"].ToString() != "")
            {
                order.price = decimal.Parse(dataTable.Rows[0]["price"].ToString());
            }
            order.remark = dataTable.Rows[0]["remark"].ToString();
            if (dataTable.Rows[0]["time"].ToString() != "")
            {
                order.time = DateTime.Parse(dataTable.Rows[0]["time"].ToString());
            }
            if (dataTable.Rows[0]["type"].ToString() != "")
            {
                order.type = int.Parse(dataTable.Rows[0]["type"].ToString());
            }
            if (dataTable.Rows[0]["disposetime"].ToString() != "")
            {
                order.disposetime = DateTime.Parse(dataTable.Rows[0]["disposetime"].ToString());
            }
            order.disposeremark = dataTable.Rows[0]["disposeremark"].ToString();
            order.carryway = dataTable.Rows[0]["carryway"].ToString();
            if (dataTable.Rows[0]["carryprice"].ToString() != "")
            {
                order.carryprice = decimal.Parse(dataTable.Rows[0]["carryprice"].ToString());
            }
            return order;
        }

        public static order GetModel(string onum)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from orders ");
            builder.Append(" where ordernumber='" + onum + "'");
            order order = new order();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            if (dataTable.Rows[0]["id"].ToString() != "")
            {
                order.id = int.Parse(dataTable.Rows[0]["id"].ToString());
            }
            order.username = dataTable.Rows[0]["username"].ToString();
            order.ordernumber = dataTable.Rows[0]["ordernumber"].ToString();
            order.name = dataTable.Rows[0]["name"].ToString();
            order.address = dataTable.Rows[0]["address"].ToString();
            order.zipcode = dataTable.Rows[0]["zipcode"].ToString();
            order.phone = dataTable.Rows[0]["phone"].ToString();
            order.email = dataTable.Rows[0]["email"].ToString();
            order.payment = dataTable.Rows[0]["payment"].ToString();
            order.userid = int.Parse(dataTable.Rows[0]["userid"].ToString()); //1.1
            if (dataTable.Rows[0]["audit"].ToString() != "") //1.1
            {
                if ((dataTable.Rows[0]["audit"].ToString() == "1") || (dataTable.Rows[0]["audit"].ToString().ToLower() == "true"))
                {
                    order.audit = true;
                }
                else
                {
                    order.audit = false;
                }
            }
            if (dataTable.Rows[0]["payed"].ToString() != "")
            {
                if ((dataTable.Rows[0]["payed"].ToString() == "1") || (dataTable.Rows[0]["payed"].ToString().ToLower() == "true"))
                {
                    order.payed = true;
                }
                else
                {
                    order.payed = false;
                }
            }
            if (dataTable.Rows[0]["send"].ToString() != "") //1.1
            {
                if ((dataTable.Rows[0]["send"].ToString() == "1") || (dataTable.Rows[0]["send"].ToString().ToLower() == "true"))
                {
                    order.audit = true;
                }
                else
                {
                    order.audit = false;
                }
            }
            if (dataTable.Rows[0]["paytime"].ToString() != "")
            {
                order.paytime = DateTime.Parse(dataTable.Rows[0]["paytime"].ToString());
            }
            if (dataTable.Rows[0]["price"].ToString() != "")
            {
                order.price = decimal.Parse(dataTable.Rows[0]["price"].ToString());
            }
            order.remark = dataTable.Rows[0]["remark"].ToString();
            if (dataTable.Rows[0]["time"].ToString() != "")
            {
                order.time = DateTime.Parse(dataTable.Rows[0]["time"].ToString());
            }
            if (dataTable.Rows[0]["type"].ToString() != "")
            {
                order.type = int.Parse(dataTable.Rows[0]["type"].ToString());
            }
            if (dataTable.Rows[0]["disposetime"].ToString() != "")
            {
                order.disposetime = DateTime.Parse(dataTable.Rows[0]["disposetime"].ToString());
            }
            order.disposeremark = dataTable.Rows[0]["disposeremark"].ToString();
            order.carryway = dataTable.Rows[0]["carryway"].ToString();
            if (dataTable.Rows[0]["carryprice"].ToString() != "")
            {
                order.carryprice = decimal.Parse(dataTable.Rows[0]["carryprice"].ToString());
            }
            return order;
        }

        public static void Update(order model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update orders set ");
            builder.Append("username='" + model.username + "',");
            builder.Append("ordernumber='" + model.ordernumber + "',");
            builder.Append("name='" + model.name + "',");
            builder.Append("address='" + model.address + "',");
            builder.Append("zipcode='" + model.zipcode + "',");
            builder.Append("phone='" + model.phone + "',");
            builder.Append("email='" + model.email + "',");
            builder.Append("payment='" + model.payment + "',");
            builder.Append("payed=" + (model.payed ? 1 : 0) + ",");
            builder.Append("paytime='" + model.paytime + "',");
            builder.Append("price=" + model.price + ",");
            builder.Append("remark='" + model.remark + "',");
            builder.Append("time='" + model.time + "',");
            builder.Append("type=" + model.type + ",");
            builder.Append("disposetime='" + model.disposetime + "',");
            builder.Append("disposeremark='" + model.disposeremark + "',");
            builder.Append("carryway='" + model.carryway + "',");
            builder.Append("carryprice=" + model.carryprice + ",");
            builder.Append("userid=" + model.userid + ",");  //1.1
            builder.Append("audit=" + (model.audit ? 1 : 0) + ","); //1.1
            builder.Append("send=" + (model.send ? 1 : 0) + ""); //1.1
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }


}

